package com.hongyun.tms.common.dto.crewcert;

import com.hongyun.tms.common.utils.DateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.time.LocalDate;

/**
 * 新增或修改证书
 *
 * @author liPeng
 * @date 2024-05-31
 **/
@Data
public class CrewCertSaveReqDto implements Serializable {
    @ApiModelProperty(value = "主键")
    private Long id;

    @ApiModelProperty(value = "船员ID")
    @NotNull(message = "船员ID不能为空")
    private Long crewId;

    @ApiModelProperty(value = "证件名称")
    @NotBlank(message = "证书名称不能为空")
    @Size(max = 20, message = "证书名称不能超过20个字符")
    private String certName;

    @ApiModelProperty(value = "证书种类ID")
    @NotBlank(message = "证件类型不能为空")
    private String certTypeId;

    @ApiModelProperty(value = "证书有效期开始日期")
    @NotBlank(message = "开始日期不能为空")
    private String beginDate;

    @ApiModelProperty(value = "结束日期")
    @NotBlank(message = "证书有效期结束日期不能为空")
    private String endDate;

    @ApiModelProperty(value = "证书编号")
    @NotBlank(message = "证书编号不能为空")
    @Size(max = 20, message = "证书编号不能超过20个字符")
    private String certNo;

    @ApiModelProperty(value = "附件URL，多个时逗号隔开")
    @NotBlank(message = "请上传证书附件")
    private String attachUrl;

    @AssertTrue(message = "起始日期不能小于截止日期")
    public boolean isBeginTimeBeforeEndTime() {
        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate)) {
            return true; // 如果其中一个为空，不进行校验
        }

        LocalDate begin = DateUtils.str2LocalDate(beginDate);
        LocalDate end = DateUtils.str2LocalDate(endDate);
        return begin.isBefore(end);
    }
}
